//
//  FritzXMLTools.m
//  AdressBookConverter
//
//  Created by Daniel Schneller on 17.12.08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "FritzXMLTools.h"
#import "Foundation/Foundation.h"


@implementation FritzXMLTools
- (NSXMLElement*)phoneElementFor:(NSString*)number andType:(NSString*)type withPrio:(int)prio {
	NSXMLElement* tXmlNumber = [NSXMLNode elementWithName:@"number"];
	NSArray* tKeys = [NSArray arrayWithObjects:@"type",@"quickdial",@"vanity",@"prio",nil];
	NSString* tPrioAsString = [NSString stringWithFormat:@"%d",prio];
	NSArray* tValues = [NSArray arrayWithObjects:type,@"",@"",tPrioAsString,nil];
	NSDictionary* tAttributes = [NSDictionary dictionaryWithObjects:tValues forKeys:tKeys];
	[tXmlNumber setAttributesAsDictionary:tAttributes];
	[tXmlNumber setStringValue:number];
	return tXmlNumber;
}
- (NSXMLElement *) contactElementFor: (NSString *) fullName officePhone: (NSString *) officePhone homePhone: (NSString *) homePhone mobilePhone: (NSString *) mobilePhone  {
	NSXMLElement* tXmlContact = [NSXMLNode elementWithName:@"contact"];
	NSXMLElement* tXmlCategory = [NSXMLNode elementWithName:@"category"];
	[tXmlCategory setStringValue:@"0"];
	NSXMLElement* tXmlServices = [NSXMLNode elementWithName:@"services"];
	NSXMLElement* tXmlSetup = [NSXMLNode elementWithName:@"setup"];
	
	NSXMLElement* tXmlPerson = [NSXMLNode elementWithName:@"person"];
	NSXMLElement* tXmlRealname = [NSXMLNode elementWithName:@"realName"];
	[tXmlRealname setStringValue:fullName];
	
	NSXMLElement* tXmlTelephony = [NSXMLNode elementWithName:@"telephony"];
	if (officePhone) {
		[tXmlTelephony addChild:[self phoneElementFor:officePhone andType:@"office" withPrio:0]];
	}
	if (homePhone) {
		[tXmlTelephony addChild:[self phoneElementFor:homePhone andType:@"home" withPrio:1]];
	}
	if (mobilePhone) {
		[tXmlTelephony addChild:[self phoneElementFor:mobilePhone andType:@"mobile" withPrio:0]];
	}
	[tXmlPerson addChild:tXmlRealname];
	[tXmlContact addChild:tXmlCategory];
	[tXmlContact addChild:tXmlServices];
	[tXmlContact addChild:tXmlSetup];
	[tXmlContact addChild:tXmlPerson];
	[tXmlContact addChild:tXmlTelephony];
	return tXmlContact;
}


-(NSString *) getPrivatePart {
	NSString* tPrivate = @"<phonebook owner=\"255\">"
"	<contact>"
"    <category/><services/><setup/>"
"    <person><realName>Sunflower</realName></person>"
"    <telephony><number type=\"intern\">610</number></telephony>"
"	</contact>"
"	<contact>"
"    <category/><services/><setup/>"
"    <person><realName>Anrufbeantworter 1</realName></person>"
"    <telephony><number type=\"intern\">600</number></telephony>"
"	</contact>"
"	<contact>"
"    <category/><services/><setup/>"
"    <person><realName /></person>"
"    <telephony><number type=\"intern\">51</number></telephony>"
"	</contact>"
"	<contact>"
"    <category/><services/><setup/>"
"    <person><realName>fax</realName></person>"
"    <telephony><number type=\"intern\">1</number></telephony>"
"	</contact>"
"	<contact>"
"    <category/><services/><setup/>"
"    <person><realName /></person>"
"    <telephony><number type=\"intern\">2</number></telephony>"
"	</contact>"
"	<contact>"
"    <category/><services/><setup/>"
"    <person><realName /></person>"
"    <telephony><number type=\"intern\">3</number></telephony>"
"	</contact>"
"	</phonebook>";
	return tPrivate;
}

@end
